package com.test.ds;

// classDataArray.java
// data items as class objects
// to run this program: C>java ClassDataApp
/////////////////////////////////////////////////////////////
class Person{
	private String lastName;
	private String firstName;
	private int age;
	//---------------------------------------------
	public Person(String last, String first, int a){
		lastName = last;
		firstName = first;
		age = a;
	} //end Person()
	//----------------------------------------------
	public void displayPerson(){
		System.out.print("  Last name: " + lastName);
		System.out.print(", First name: " + firstName);
		System.out.print(", Age: " + age);
	} // end displayPerson()
	//----------------------------------------------
	public String getLast(){
		return lastName;
	} 
} // end class Person
////////////////////////////////////////////////////////////
class ClassDataArray{
	private Person[] a;
	private int nElems;
	
	public ClassDataArray(int max){
		a = new Person[max];
		nElems = 0;
	}
	//----------------------------------------------
	public Person find(String searchName){
		int j;
		for(j=0; j<nElems; j++){
			if( a[j].getLast().equals(searchName)){
				break;
			}
		}
		if(j == nElems){
			return null;
		}else{
			return a[j];
		}
	}
	//-----------------------------------------------
	public void insert(String last, String first, int age){
		a[nElems] = new Person(last, first, age);
		nElems++;
	}
	//-----------------------------------------------
	public boolean delete(String searchName){
		int j;
		for(j=0; j<nElems; j++){
			if(a[j].getLast().equals(searchName)){
				break;
			}
		}
		if(j == nElems){
			return false;
		}else{
			for(int k=j; k<nElems; k++){
				a[k] = a[k+1];
			}
			nElems--;
			return true;
		}
	}
	//------------------------------------------------
	public void displayA(){
		for(int j =0; j<nElems; j++){
			a[j].displayPerson();
		}
	}
} // end class ClassDataArray
//////////////////////////////////////////////////////////
class ClassDataApp{
	public static void main(String[] args){
		int maxSize = 100;
		ClassDataArray arr;
		arr = new ClassDataArray(maxSize);
		
		arr.insert("Evans", "Patty", 24);
		arr.insert("Smith", "Lorraine", 37);
		arr.insert("Yee", "Tom", 43);
		arr.insert("Adams", "Henry", 43);
		arr.insert("Hashimoto", "Sato", 63);
		arr.insert("Stimson", "Henry", 29);
		arr.insert("Velasquez", "Jose", 72);
		arr.insert("Lamarque", "Henry", 54);
		arr.insert("Vang", "Minh", 22);
		arr.insert("Creswell", "Lucinda", 18);
		
		arr.displayA();
		
		String searchKey = "Stimson";
		Person found;
		found = arr.find(searchKey);
		if(found!=null){
			System.out.print("Found ");
			found.displayPerson();
		}else{
			System.out.println("Can't find " + searchKey);
		}
		
		System.out.println("Deleting Smith, Yee and Creswell");
		arr.delete("Smith");
		arr.delete("Yee");
		arr.delete("Creswell");
		
		arr.displayA();
		
	} // end main()
} // end class ClassDataApp